package com.kevin.websocket.client.controller;

import com.kevin.websocket.client.configuration.util.JsonUtil;
import com.kevin.websocket.client.configuration.websocket.WebSocketClient;
import com.kevin.websocket.client.configuration.websocket.properties.WebsocketProperties;
import com.kevin.websocket.client.model.dto.MessageDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@Tag(name = "Websocket客户端")
@RequestMapping("/websocket/client")
public class WebsocketController {

    @Resource
    private WebsocketProperties properties;

    @Resource
    private WebSocketClient webSocketClient;

    @Operation(summary = "获取客户端ID", description = "获取客户端ID")
    @GetMapping("/id")
    public String id() {
        return properties.getId();
    }

    @Operation(summary = "向服务端发布消息", description = "向服务端发布消息")
    @PostMapping("/publish")
    public void publish(@RequestBody MessageDTO messageDTO) {
        webSocketClient.getConnection().send(JsonUtil.objectToJson(messageDTO));
        log.debug("publish : {}", messageDTO);
    }

}
